package cn.tedu.charging.order;

import cn.tedu.charging.order.pojo.param.OrderAddParam;
import cn.tedu.charging.common.web.JsonResult;
import cn.tedu.charging.common.pojo.vo.UserInfoVO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

@SpringBootTest
public class RestTemplateTestes {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void testRestTemplate() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://www.baidu.com/", String.class);
        String body = forEntity.getBody();
        System.out.println(body);
    }

    @Test
    public void testRestTemplateGetUser() {
//        String url = "http://localhost:8083/user/info/";后面直接加
        String url = "http://localhost:8083/user/info/{1}";//用占位符传参
        ResponseEntity<JsonResult> entity = restTemplate.getForEntity(url, JsonResult.class,1);
        if (entity!=null){
            HttpStatus statusCode = entity.getStatusCode();
            if (HttpStatus.OK == statusCode){
                JsonResult body = entity.getBody();
                System.out.println(body);
            }
        }
    }

    /**
     *ParameterizedTypeReference 返回指定的类型,无需我们自己反序列化
     * json 转成java exchange
     */
    @Test
    public void testRestTemplateExchangeGetUser(){
        //String url = "http://localhost:8083/user/info/1";

        String url = "http://localhost:8083/user/info/{1}"; //占位符号

        ParameterizedTypeReference<JsonResult<UserInfoVO>> reposeBody =
                new ParameterizedTypeReference<JsonResult<UserInfoVO>>() {
                };

        ResponseEntity<JsonResult<UserInfoVO>> entity = restTemplate
                .exchange(url, HttpMethod.GET, null, reposeBody, 1);
        JsonResult<UserInfoVO> body = entity.getBody();
        UserInfoVO data = body.getData();
        Integer code = body.getCode();
        String msg = body.getMsg();

        System.out.println(data);


    }


    @Test
    public void testRestTemplatePostOrder() {
//        String url = "http://localhost:8083/user/info/";后面直接加
        String url = "http://localhost:8080/order/create/";//用占位符传参
        OrderAddParam orderAddParam = new OrderAddParam();
        orderAddParam.setPileId(1);
        orderAddParam.setGunId(2);
        orderAddParam.setUserId(3L);

        ResponseEntity<JsonResult> entity = restTemplate.postForEntity(url, orderAddParam,JsonResult.class);
        if (entity!=null){
            HttpStatus statusCode = entity.getStatusCode();
            if (HttpStatus.OK == statusCode){
                JsonResult body = entity.getBody();
                System.out.println(body);
            }
        }
    }

    /**
     * 订单服务  1台
     * 用户服务有 3台 8083 8084 8085
     *
     * 负载均衡算法
     *
     * 最少连接算法
     * 均衡
     * 用户 1,2,3  订单 --> 8083 3个链接
     * 用户 2,3,4  订单 --> 8084 3个链接
     * 用户 5  订单 --> 8085 1 个链接     8085 最清闲
     *
     * 用户 6  订单 --> 8085 1+1 = 2
     *
     * 随机
     * 优点简单 不太均衡
     * 用户 1,2,3  订单 --> 8083
     * 用户 2,3,4  订单 --> 8084
     * 用户 5  订单 --> 8085
     *
     * 用户6  随机  8083
     *
     * 轮询
     * 挨着来,不管当前用户服务的状态,就算你当前处理的很慢,轮到你,你也得上,平均,负载不太均衡
     * 用户 1  订单 --> 8083
     * 用户 2  订单 --> 8084
     * 用户 3  订单 --> 8085
     * 用户 4  订单 --> 8083
     *
     * 加权 优先级更高的用户服务能获取到相对更高的负载 ,服务器性能好的,给更高的优先级
     * 用户 1  订单 --> 8083  2
     * 用户 2  订单 --> 8083  1
     * 用户 3  订单 --> 8085  1
     *
     *
     *
     */
    @Test
    public void testRestTemplateExchangeGetUserMulti(){
        String url = "http://localhost:8083/user/info/{1}";
        String url1 = "http://localhost:8084/user/info/{1}";
        String url2 = "http://localhost:8085/user/info/{1}";
        String[] servers = new String[3];
        servers[0] = url;
        servers[1] = url1;
        servers[2] = url2;
        Random random = new Random();
        int r = random.nextInt(servers.length);

        String server = servers[r];

        ParameterizedTypeReference<JsonResult<UserInfoVO>> reposeBody =
                new ParameterizedTypeReference<JsonResult<UserInfoVO>>() {
                };

        ResponseEntity<JsonResult<UserInfoVO>> entity = restTemplate.
                exchange(server, HttpMethod.GET, null, reposeBody, 1);
        JsonResult<UserInfoVO> body = entity.getBody();
        UserInfoVO data = body.getData();
        Integer code = body.getCode();
        String msg = body.getMsg();

        System.out.println(data);
    }

    @Test
    public void testRandom(){

        String url = "http://localhost:8083/user/info/{1}"; //占位符号
        String url1 = "http://localhost:8084/user/info/{1}"; //占位符号
        String url2 = "http://localhost:8085/user/info/{1}"; //占位符号

        //负载均衡算法 简单实现随机
        String[] servers = new String[3];
        servers[0] = url;
        servers[1] = url1;
        servers[2] = url2;

        // servers[7] //数组越界
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            //Random random = new Random();
            int r = random.nextInt(servers.length);
            String server = servers[r];
            System.out.println(i+ ":" + r + ":" + server);
        }
    }
}
